﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.View.Section.ashx
{
    /// <summary>
    /// Section 的摘要说明
    /// </summary>
    public class Section : IHttpHandler
    {
        private static readonly SectionInformationService ProxySectionInformation = ObjectFactory<SectionInformationService>.Create();
        private static readonly SectionLinkService ProxySectionLink = ObjectFactory<SectionLinkService>.Create();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string result = string.Empty;
            string action = context.Request["action"] ?? string.Empty;

            switch (action)
            {
                case "addSectionInformation":
                    result = addSectionInformation(context);
                    break;
                case "getSectionInformation":
                    result = getSectionInformation(context);
                    break;
                case "addSectionLink":
                    result = addSectionLink(context);
                    break;
                case "getSectionLink":
                    result = getSectionLink(context);
                    break;
                default: break;
            }

            context.Response.Write(result);
        }


        public string addSectionInformation(HttpContext context)
        {
            SectionInformation sectioninfomaintion;
            try
            {
                string ID = context.Request["ID"] != null ? context.Request["ID"].ToString() : string.Empty;
                string SectionID = context.Request["SectionID"] != null ? context.Request["SectionID"].ToString() : string.Empty;
                string Title = context.Request["Title"] != null ? context.Request["Title"].ToString() : string.Empty;
                string OrderIndex = context.Request["OrderIndex"] != null ? context.Request["OrderIndex"].ToString() : string.Empty;
                string Content = context.Request.Form["Content"] != null ? context.Request.Form["Content"].ToString() : string.Empty;

                bool IsAdd = false;
                if (string.IsNullOrEmpty(ID)) //添加
                {
                    sectioninfomaintion = new SectionInformation();
                    sectioninfomaintion.ID = Guid.NewGuid();
                    IsAdd = true;
                }
                else//更新
                {
                    sectioninfomaintion = ProxySectionInformation.Get(new Guid(ID));
                    if (sectioninfomaintion == null)
                    {
                        return string.Format("false|Sectioninfomaintion no such {0}", sectioninfomaintion.ID);
                    }
                }


                sectioninfomaintion.SectionID = new Guid(SectionID);
                sectioninfomaintion.Title = Title;
                sectioninfomaintion.OrderIndex = int.Parse(OrderIndex);
                sectioninfomaintion.Content = Content;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    return string.Format("false|未登录或者没有权限操作");
                    //Response.Redirect("~/Account/Login.aspx");
                }
                sectioninfomaintion.Operator = op;

                sectioninfomaintion.OperatorTime = DateTime.Now;
                sectioninfomaintion.Status = 1;

                if (IsAdd) //添加
                {
                    ProxySectionInformation.Insert(sectioninfomaintion);
                }
                else      //更新
                {
                    ProxySectionInformation.Update(sectioninfomaintion);
                }
            
            }
            catch (Exception ex)
            {
                return string.Format("false|{0}", ex.ToString());
            }

            return string.Format("true|{0}",sectioninfomaintion.ID);

            


        }

        public string getSectionInformation(HttpContext context)
        {
            string ID = context.Request["ID"].ToString();
            string SectionID = context.Request["SectionID"].ToString();
            var sectionInformation = ProxySectionInformation.GetBySectionId(new Guid(SectionID));

            string jsonStr = string.Empty;
            if (sectionInformation != null)
            {
                jsonStr = JSON.Serialize(sectionInformation);
            }
            return string.Format("true|{0}", jsonStr);
        }

        public string addSectionLink(HttpContext context)
        {
            SectionLink sectionLink;
            try
            {
                string ID = context.Request["ID"] != null ? context.Request["ID"].ToString() : string.Empty;
                string SectionID = context.Request["SectionID"] != null ? context.Request["SectionID"].ToString() : string.Empty;
                string LinkUrl = context.Request["LinkUrl"] != null ? context.Request["LinkUrl"].ToString() : string.Empty;
          

                bool IsAdd = false;
                if (string.IsNullOrEmpty(ID)) //添加
                {
                    sectionLink = new SectionLink();
                    sectionLink.ID = Guid.NewGuid();
                    IsAdd = true;
                }
                else//更新
                {
                    sectionLink = ProxySectionLink.Get(new Guid(ID));
                    if (sectionLink == null)
                    {
                        return string.Format("false|sectionLink no such {0}", sectionLink.ID);
                    }
                }

 
                sectionLink.SectionID = new Guid(SectionID);
                sectionLink.LinkUrl = LinkUrl;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    return string.Format("false|未登录或者没有权限操作");
                   // Response.Redirect("~/Account/Login.aspx");
                }
                sectionLink.Opeator = op;

                sectionLink.OpeatorTime = DateTime.Now;
                sectionLink.Status = 1;

                if (IsAdd) //添加
                {
                    ProxySectionLink.Insert(sectionLink);
                }
                else      //更新
                {
                    ProxySectionLink.Update(sectionLink);
                }

            }
            catch (Exception ex)
            {
                return string.Format("false|{0}", ex.ToString());
            }

            return string.Format("true|{0}", sectionLink.ID);

        }

        public string getSectionLink(HttpContext context)
        {
            string ID = context.Request["ID"].ToString();
            string SectionID = context.Request["SectionID"].ToString();
            var SectionLink = ProxySectionLink.GetBySectionId(new Guid(SectionID));

            string jsonStr = string.Empty;
            if (SectionLink != null)
            {
              jsonStr =  JSON.Serialize(SectionLink);
            }
            return string.Format("true|{0}", jsonStr);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}